﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LX.Core.Validation;
using S4Store.Persistent;
using System.Data.Entity;

namespace S4Store.Service
{
    public class BaseService
    {
        private Transtion _transtion;

        public void SetTranstion(Transtion value)
        {
            _transtion = value;
        }

        public Transtion GetTranstion()
        {
            return _transtion;
        }

        

        public IQueryable<IEntity> GetQuery<IEntity>() where IEntity : class
        {
            return GetTranstion().PersistentContext.Set<IEntity>().AsQueryable();
        }

        public DbSet<IEntity> GetSet<IEntity>() where IEntity : class
        {
            return GetTranstion().PersistentContext.Set<IEntity>();
        }

        protected void Valid<TEntity>(TEntity entity,IValidExpression<TEntity> validexpression)
        {
            var result=validexpression.Valid(entity);
            if (result.IsValida) return;
            throw new Exception(result.Summry);
        }
    }
}
